package com.ctshk.common.sms.core;

import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.ctshk.common.sms.constant.SmsCode;
import com.ctshk.common.sms.constant.SmsConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 短信发送
 * </p>
 *
 * @author 李新科
 * @date 2021/4/22 11:09
 */
public class SmsUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(SmsUtil.class);

    private static String accessKeyId = "LTAI4GGyDkvxfctCFywzWKac";

    private static String accessKeySecret = "LcQ2m03p2xiNxqjXsCq7WOY484IUtj";

    private static String signName = "港中旅";

    private static String verificationTemplateCode = "SMS_215795871";

    private static String endpoint = "dysmsapi.aliyuncs.com";

    private static String domesticCode = "+86";

    private static String domesticCode2 = "86";

    private static Client client;

    static {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = endpoint;
        try {
            client = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 是否是国内区号
     *
     * @param contactMobileAreaCode
     * @return
     */
    public static boolean isDomestic(String contactMobileAreaCode) {
        return domesticCode.equals(contactMobileAreaCode) || domesticCode2.equals(contactMobileAreaCode);
    }


    public static void sendSms(String phone, String signName, String templateCode, String templateParam) throws Exception {
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setTemplateParam(templateParam);

        SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
        String code = sendSmsResponse.getBody().getCode();
        if (!SmsCode.OK.getCode().equals(code)) {
            String msg = String.format("手机号：%s,短信发送失败,错误信息: %s", phone, sendSmsResponse.getBody().getMessage());
            LOGGER.error(msg);
            throw new RuntimeException(SmsCode.getMsgByCode(code));
        }

    }

    public static void sendSms(String phone, String signName, String templateCode, Map<String, String> templateParamMap) throws Exception {
        String templateParam = JSONUtil.toJsonStr(templateParamMap);
        sendSms(phone, signName, templateCode, templateParam);
    }

    public static void sendSms(String phone, String templateCode, Map<String, String> templateParamMap) throws Exception {
        String templateParam = JSONUtil.toJsonStr(templateParamMap);
        sendSms(phone, signName, templateCode, templateParam);
    }

    public static void sendSmsCode(String phone, String code) throws Exception {
        String templateParam = "{\"code\":\"" + code + "\"}";
        sendSms(phone, signName, verificationTemplateCode, templateParam);
    }

    public static void sendSmsCode(String phone, String verificationTemplateCode, String code) throws Exception {
        String templateParam = "{\"code\":\"" + code + "\"}";
        sendSms(phone, signName, verificationTemplateCode, templateParam);
    }

    public static void sendSmsCode(String phone, String signName, String verificationTemplateCode, String code) throws Exception {
        String templateParam = "{\"code\":\"" + code + "\"}";
        sendSms(phone, signName, verificationTemplateCode, templateParam);
    }

    public static void main(String[] args) throws Exception{
        Map<String,String> hashMap = new HashMap();
        hashMap.put("code","0973");
        sendSms("15989306469","SMS_215795871",hashMap);
    }

}
